import React from 'react';
import {
  Form,
  Input,
  Button,
  DatePicker,
  Select,
  Table,
  Icon,
  Radio,
  InputNumber,
  Checkbox,
  Modal,
  message,
  notification,
  Affix,
  Upload
} from 'antd';

import InnerTable from 'components/DBTable/InnerTable.js';
import ajax from '../../../utils/ajax';
import DisRole from './DisRole.js';

const FormItem = Form.Item;
const ButtonGroup = Button.Group;
var _this;

/**
 * 内部表格组件
 */
class UserInnerTable extends InnerTable {

  constructor(props) {
    super(props);
    this.state.modalDisRoleVisible = false;
    this.state.roleChild = [];  //分配角色时使用
  }

  enableFieldToFrom = (field) => {
    if (this.state.modalState == this.modalActionState.Update) {
      if (field.key == 'plainPassword') return false;
    }
    return true;
  }

  prepareToolBar = function () {
    let isButtondisabled = false;

    if (this.state.selectedRowKeys.length > 0) {  // 是否选择
      isButtondisabled = true;
    }

    if (this.state.selectedRowKeys.length > 1) {  // 是否选择了多项
      isButtondisabled = false;
    }

    var items = [];

    if (this.props.tableConfig.showPsdRet) {
      items.push(<Button key='resetPwdBtn' disabled={!isButtondisabled} onClick={this.onClickResetPsd}>
        <Icon type="edit" />重置密码
      </Button>)
    }

    if (this.props.tableConfig.showUpdateStatus) {
      items.push(<Button key='updateStatusBtn' disabled={!isButtondisabled} onClick={this.onClickResetStatu}>
        <Icon type="edit" />更新账户状态
      </Button>)
    }

    if (this.props.tableConfig.showRoleAssign) {
      items.push(<Button key='roleAssignBtn' disabled={!isButtondisabled} onClick={this.onClickDisRole}>
        <Icon type="user" />分配角色
      </Button>)
    }

    return items;
  }

  //用户重置密码
  onClickResetPsd = (e) => {
    e.preventDefault();
    Modal.confirm({
      title: '重置密码',
      content: "确定重置密码为123456?",
      onOk: () => {
        this.handleRetPsd();
      },
    });

  }

  //真正去重置
  handleRetPsd = () => {
    const url = `/${this.props.tableName}/reset/password/${this.state.selectedRowKeys.join(',')}`;
    const hide = message.loading('正在重置...', 0);
    _this = this;
    ajax.post({
      url: url,
      success: function (res) {
        if (res.errorCode == 200) {
          hide();
          notification.success({
            message: '重置成功',
            description: `更新1条数据`,
          });
          // 更新数据后, 注意刷新下整个页面
          _this.props.refresh();
        }
        else {
          notification.success({
            message: '重置失败',
            description: `请联系管理员处理`,
          });
        }

      }
    })

  }


  //更新账户状态
  onClickResetStatu = (e) => {
    e.preventDefault();
    Modal.confirm({
      title: '确认更新账户状态',
      content: `${this.state.selectedRowKeys.join(', ')}`,
      onOk: () => {
        this.handleRetStatu();
      },
    });

  }

  //真正去更新状态
  handleRetStatu = () => {

    const url = `/${this.props.tableName}/reset/status/${this.state.selectedRowKeys.join(',')}`;
    const hide = message.loading('正在更新...', 0);
    //obj[this.primaryKey] = undefined;

    var _this = this;

    ajax.post({
      url: url,
      success: function (res) {
        if (res.errorCode == 200) {
          hide();
          notification.success({
            message: '更新成功',
            description: `更新1条数据`,
          });
          // 更新数据后, 注意刷新下整个页面
          _this.props.refresh();
        }
        else {
          notification.success({
            message: '更新失败',
            description: `请联系管理员处理`,
          });
        }

      }
    })
  }

  //用户管理分配角色
  onClickDisRole = () => {
    delete this.state.roleUserChild;
    this.setState({ modalDisRoleVisible: true });
  }

  handleModalDisRoleOk =()=>{

    //处理角色数据
    let tempObj=this.state.roleIds;
    for(let i=0;i<tempObj.length;i++){
      tempObj[i]=parseInt(tempObj[i]);
    }

    let temp=JSON.stringify(tempObj);
    let roleIdLength=temp.length;
    let roleid=temp.substring(1,temp.length-1);

    _this = this;
     ajax.post({
       url:"/user/assignRole",
       params:{userId:this.state.selectedRowKeys.join(""),roleId:roleid},
       success:function(res){         
          if(res.errorCode==200){
             delete _this.state.roleUserChild;
             _this.props.refresh(); //刷新列表
             message.success('修改成功');    
          }else{
            notification.error({
              message: '出错啦!',
              description: res.moreInfo,
              duration: 0,
            });
          }
         _this.setState({ modalDisRoleVisible: false});
       }
     })
  }

  hideModalDisRole = () => {
    this.setState({ modalDisRoleVisible: false });
  }

  updateRole =(value)=>{
    this.setState({ roleIds: value,roleUserChild:value});
  }

  //分配角色modal
  getUniqueModal = () => {
    return (
      <div>
        <Modal title="分配角色" visible={this.state.modalDisRoleVisible} onOk={this.handleModalDisRoleOk}
          onCancel={this.hideModalDisRole}>
          <DisRole updateId={this.state.selectedRowKeys} updateRole={this.updateRole} defaultValue={this.state.roleUserChild}/>
        </Modal>
      </div>);
  }
}

export default UserInnerTable;
